from app import db
from datetime import datetime

class GepResult(db.Model):
    """GEP核算结果模型"""
    __tablename__ = 'gep_results'
    
    id = db.Column(db.Integer, primary_key=True)
    year = db.Column(db.Integer, nullable=False, comment='核算年份')
    gepCity = db.Column(db.String(50), nullable=False, comment='市级地区名称')
    gepDistrict = db.Column(db.String(50), nullable=False, comment='区级地区名称')
    gepTown = db.Column(db.String(50), nullable=False, comment='镇级地区名称')
    gepResult = db.Column(db.Float, comment='总生态产品价值核算结果（万元）')
    materialSupplyValue = db.Column(db.Float, comment='物质供给核算结果（万元）')
    cleanEnergyValue = db.Column(db.Float, comment='清洁能源核算结果（万元）')
    waterResourcesValue = db.Column(db.Float, comment='水资源价值核算结果（万元）')
    tourismHealthValue = db.Column(db.Float, comment='旅游康养价值核算结果（万元）')
    leisureRecreationValue = db.Column(db.Float, comment='休闲游憩价值核算结果（万元）')
    landscapeAppreciationValue = db.Column(db.Float, comment='景观增值核算结果（万元）')
    creator = db.Column(db.String(50), comment='数据创建者用户名')
    updater = db.Column(db.String(50), comment='数据更新者用户名')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='数据创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='数据更新时间')
    
    def __repr__(self):
        return f'<GepResult {self.year}-{self.gepCity}-{self.gepDistrict}-{self.gepTown}>'
    
    def to_dict(self):
        return {
            'id': self.id,
            'year': self.year,
            'gepCity': self.gepCity,
            'gepDistrict': self.gepDistrict,
            'gepTown': self.gepTown,
            'gepResult': self.gepResult,
            'materialSupplyValue': self.materialSupplyValue,
            'cleanEnergyValue': self.cleanEnergyValue,
            'waterResourcesValue': self.waterResourcesValue,
            'tourismHealthValue': self.tourismHealthValue,
            'leisureRecreationValue': self.leisureRecreationValue,
            'landscapeAppreciationValue': self.landscapeAppreciationValue,
            'creator': self.creator,
            'updater': self.updater,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        } 